package com.netease.nim.demo.team.viewholder;

import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.GlideUtil;
import com.netease.nim.demo.NimCache;
import com.netease.nim.demo.common.adapter.TViewHolder;
import com.netease.nim.demo.common.ui.imageview.HeadImageView;
import com.netease.nim.demo.contact.cache.TeamDataCache;
import com.netease.nim.demo.team.activity.TeamInfoActivity;
import com.netease.nim.demo.team.adapter.TeamMemberAdapter;
import com.shwread.android.qysw10000227.R;

import logic.util.Util;
import logic.util.Utils;


public class TeamMemberHolder
		extends
		TViewHolder
{

	public final static String               OWNER = "owner";
	public final static String               ADMIN = "admin";
	protected TeamMemberHolderEventListener teamMemberHolderEventListener;
	protected ItemClickListener itemClickListener;
	private HeadImageView                    headImageView;
	private ImageView                        ownerImageView;
	private ImageView                        adminImageView;
	private ImageView                        deleteImageView;
	private TextView                         nameTextView;
	private TeamMemberAdapter.TeamMemberItem memberItem;

	public void setEventListener(TeamMemberHolderEventListener eventListener) {
		this.teamMemberHolderEventListener = eventListener;
	}

	public void setItemClickListener(ItemClickListener listener) {
		itemClickListener = listener;
	}

	protected TeamMemberAdapter getAdapter() {
		return (TeamMemberAdapter) super.getAdapter();
	}

	@Override
	protected int getResId() {
		return R.layout.team_member_item;
	}

	@Override
	protected void inflate() {
		headImageView = (HeadImageView) view.findViewById(R.id.imageViewHeader);
		nameTextView = (TextView) view.findViewById(R.id.textViewName);
		ownerImageView = (ImageView) view.findViewById(R.id.imageViewOwner);
		adminImageView = (ImageView) view.findViewById(R.id.imageViewAdmin);
		deleteImageView = (ImageView) view.findViewById(R.id.imageViewDeleteTag);
	}

	@Override
	protected void refresh(Object item) {
		memberItem = (TeamMemberAdapter.TeamMemberItem) item;
		headImageView.resetImageView();
		ownerImageView.setVisibility(View.GONE);
		adminImageView.setVisibility(View.GONE);
		deleteImageView.setVisibility(View.GONE);

		if (getAdapter().getMode() == TeamMemberAdapter.Mode.NORMAL)
		{
			view.setVisibility(View.VISIBLE);
			if (memberItem.getTag() == TeamMemberAdapter.TeamMemberItemTag.ADD)
			{
				// add team member
				headImageView.setBackgroundResource(R.drawable.team_member_add_selector);
				nameTextView.setText(context.getString(R.string.add));
				headImageView.setOnClickListener(new View.OnClickListener()
				{
					@Override
					public void onClick(View v) {
						getAdapter().getAddMemberCallback().onAddMember();
					}
				});
			}
			else if (memberItem.getTag() == TeamMemberAdapter.TeamMemberItemTag.DELETE)
			{
				// delete team member
				headImageView.setBackgroundResource(R.drawable.team_member_delete_selector);
				nameTextView.setText(context.getString(R.string.remove));
				headImageView.setOnClickListener(new View.OnClickListener()
				{
					@Override
					public void onClick(View v) {
						getAdapter().setMode(TeamMemberAdapter.Mode.DELETE);
						getAdapter().notifyDataSetChanged();
					}
				});
			}
			else
			{
				// show team member
				refreshTeamMember(memberItem, false);
			}
		}
		else if (getAdapter().getMode() == TeamMemberAdapter.Mode.DELETE)
		{
			if (memberItem.getTag() == TeamMemberAdapter.TeamMemberItemTag.NORMAL)
			{
				refreshTeamMember(memberItem, true);
			}
			else
			{
				view.setVisibility(View.GONE);
			}
		}
	}

	private void refreshTeamMember(final TeamMemberAdapter.TeamMemberItem item, boolean deleteMode) {
		if (Utils.isNotEmpty(item.getStaffName()))
		{
			nameTextView.setText(TeamDataCache.getInstance().getDisplayNameWithoutMe(item.getTid(), item.getStaffName()));
		}
		else if (Utils.isNotEmpty(item.getNickName()))
		{
			nameTextView.setText(TeamDataCache.getInstance().getDisplayNameWithoutMe(item.getTid(), item.getNickName()));
		}
		else
		{
			nameTextView.setText(TeamDataCache.getInstance().getDisplayNameWithoutMe(item.getTid(), item.getAccount()));
		}
		//        nameTextView.setText(TeamDataCache.getInstance().getTeamMemberDisplayName(item.getTid(), item.getAccount()));
		headImageView.loadBuddyHeadImage(item.getAccount());
		if (Util.isNotEmpty(item.getHeadImage()))
		{
			GlideUtil.load(context,item.getHeadImage(), headImageView);
		}
		headImageView.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v) {
				if (teamMemberHolderEventListener != null)
				{
					teamMemberHolderEventListener.onHeadImageViewClick(item.getAccount());
				}
				if (null != itemClickListener) itemClickListener.onClick(item);
			}
		});
		if (TeamInfoActivity.isTeamHeader == 1)
		{
			headImageView.setOnLongClickListener(new OnLongClickListener()
			{
				@Override
				public boolean onLongClick(View v) {
					if (itemClickListener != null)
					{
						itemClickListener.onLongClick(item);
						return true;
					}
					return false;
				}
			});
		}

		//        if (item.getDesc() != null) {
		//            if (item.getDesc().equals(OWNER)) {
		//                ownerImageView.setVisibility(View.VISIBLE);
		//            } else if (item.getDesc().equals(ADMIN)) {
		//                adminImageView.setVisibility(View.VISIBLE);
		//            }
		//        }
		ownerImageView.setVisibility(item.getTeamRole() == 3 ? View.VISIBLE : View.GONE);

		final String account = item.getAccount();
		if (deleteMode && !isSelf(account) && item.getTeamRole() != 3)
		{
			deleteImageView.setVisibility(View.VISIBLE);
			deleteImageView.setOnClickListener(new View.OnClickListener()
			{
				@Override
				public void onClick(View v) {
					getAdapter().getRemoveMemberCallback().onRemoveMember(account);
				}
			});
		}
		else
		{
			deleteImageView.setVisibility(View.GONE);
		}
	}

	private boolean isSelf(String account) {
		return account.equals(NimCache.getAccount());
	}

	public interface TeamMemberHolderEventListener
	{
		void onHeadImageViewClick(String uid);
	}

	public interface ItemClickListener
	{
		void onClick(TeamMemberAdapter.TeamMemberItem item);

		void onLongClick(TeamMemberAdapter.TeamMemberItem item);
	}
}
